package unteractivities.p3_fitnessapp;


import java.util.ArrayList;
import java.util.List;

import model.p3_fitnessapp.MySQLiteHelper;
import model.p3_fitnessapp.TrainingPlan;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import controler.p3_fitnessapp.*;

/**
 * 
 * @author maxkuhlmay
 * 
 */
public class UActChooseTrainingplan extends Activity {
	
	//FIXME Klicken öffnet neue Seite mit den Übungen zum Trainingsplan

	/*
	 * ==========================================================================
	 * Globale Variablen
	 * ========================================================
	 */

	List<TrainingPlan> _trainingPlan = new ArrayList<TrainingPlan>();
	List<String> _trainingPlanListView = new ArrayList<String>();

	int z = 0;

	/*
	 * ==========================================================================
	 * Android Methoden ========================================================
	 */

	/**
	 * 
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.uact_choose_trainingplan);

	}

	/**
	 * Liste wird befuellt
	 */
	@Override
	protected void onResume() {
		super.onResume();

		/*
		 * Methodenvariablen
		 */

		/*
		 * Adapter einstellen, ListView finden
		 */
		ArrayAdapter<String> adapterTrainingsplan = new ArrayAdapter<String>(
				this, android.R.layout.simple_list_item_1, _trainingPlanListView);

		MySQLiteHelper dbTrainingsplan = new MySQLiteHelper(
				getApplicationContext());

		_trainingPlan = dbTrainingsplan.getAllTrainingplans();
		_trainingPlanListView.clear();

		z = 0;

		for (TrainingPlan tp : _trainingPlan) {

			String tpData = tp.getName();
			int tpId = tp.getList_id();
			_trainingPlanListView.add(tpData);

			Log.d(null, "z : " + z);
			tp.setList_id(z);
			// ((TrainingPlan) _trainingPlan).setList_id(z);

			// ((TrainingPlan) _trainingPlan).setList_id(z);
			// Log.d(null, "ID : " + ((TrainingPlan)
			// _trainingPlan).getList_id());
			z++;
		}

		final ListView tpListView = (ListView) findViewById(R.id.list_traininplan);

		tpListView.setAdapter(adapterTrainingsplan);

		tpListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

				int i = 0;

				/*
				 * Loop ueber den Trainingsplan Wenn die gewählte ID gleich der
				 * eingegebenen ist, wird sie ausgegeben ansonsten wird die ID 1
				 * erhöht
				 */

				Log.d(null, "arg1 " + arg1);
				Log.d(null, "arg2 " + arg2);
				Log.d(null, "arg3 " + arg3);

				Log.d(null, "Selected Item : " + arg2 );

				Intent intent = new Intent(); 

				intent.setClassName(getApplicationContext().getPackageName(),
						"unteractivities.p3_fitnessapp.UActTPChooseMuscleGroup"); 
//						"controler.p3_fitnessapp.ActTest");

				intent.putExtra("selected",
						tpListView.getAdapter().getItem(arg2).toString());

				intent.putExtra("test", "test");

				for (TrainingPlan tp_List : _trainingPlan) {
//					Log.d(null, "arg 2  : " + arg2);
//
//					Log.d(null, "id test: " + tp_List.getList_id());
//
//					Log.d(null, "name test: " + tp_List.getName());

					if (tp_List.getList_id() == arg2) {

					Log.d(null, "arg 2 gleich");

						Log.d(null, "" + tp_List.getList_id());
//						intent.putExtra(tp_List.getList_id());
						intent.putExtra("tp_id", tp_List.getId());
						
						
					} else {
						Log.d(null, "arg2 ungleich");
					}
					i++;
				}

				startActivity(intent);

			}
		});


		// tpListView.setOnItemClickListener(new OnItemClickListener() {
		//
		// @Override
		// public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
		// long arg3) {
		//
		// Log.d(null, "ItemClicked " + arg2);
		//
		// Intent intent = new Intent();
		//
		// Log.d(null, "Intent created " + intent);
		//
		// intent.setClassName(getPackageName(), getPackageName()
		// + "ActTest");
		//
		// Log.d(null, "Intent filled ");
		//
		// intent.putExtra("selected",
		// tpListView.getAdapter().getItem(arg2).toString());
		//
		// // startActivity(intent);
		// startActivity(intent);
		//
		// Log.d(null, arg2 + " clicked");

		//
		// }
		//
		// });

		/*
		 * Daten holen
		 */


	}

	/*
	 * ==========================================================================
	 * Benutzer Interaktion (Buttons ... )
	 * ========================================================
	 */

	/*
	 * ==========================================================================
	 * Eigene Methoden ========================================================
	 */

	/*
	 * ==========================================================================
	 * Debug Methoden ========================================================
	 */

	/*
	 * ==========================================================================
	 * Getter und Setter
	 * ========================================================
	 */

}
